package eventos;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import memento.IMemento;

import controlador.ControladorEditor;

import vista.objetos.GraficableConEtiqueta;
import vista.paneles.hojaDibujo.HojaDeDibujo;
import vista.treeDiagrama.PopupMenuEditar;

public class EventoEtiquetaDeGraficable implements MouseListener, MouseMotionListener {

	private HojaDeDibujo hoja;
	private GraficableConEtiqueta graficable;
	private IMemento memento = null;
	private Point posicionActual = new Point();
	private boolean seEstaMoviendo = false;
	
	public EventoEtiquetaDeGraficable(HojaDeDibujo hoja, GraficableConEtiqueta graficable)
	{
		this.hoja = hoja;
		this.graficable = graficable;
	}
	
	@Override
	public void mouseDragged(MouseEvent mouse) {
	 
    	Point puntoAMover = new Point(mouse.getPoint().x-posicionActual.x, mouse.getPoint().y-posicionActual.y );
	 	
    	graficable.dragGraficable(puntoAMover);

    	hoja.getCanvas().repaint();
    	
    	this.setSeEstaMoviendo(true);
	}

	@Override
	public void mouseMoved(MouseEvent event) 
	{

	}

	@Override
	public void mouseClicked(MouseEvent event) 
	{
		
	}

	@Override
	public void mouseEntered(MouseEvent event)
	{
		
	}

	@Override
	public void mouseExited(MouseEvent event)
	{
		
	}

	@Override
	public void mousePressed(MouseEvent event) 
	{
		this.posicionActual = event.getPoint();
		this.memento = this.graficable.getMementoGenerico();
		showPopUp(event);
	}

	@Override
	public void mouseReleased(MouseEvent event) 
	{
		if (isSeEstaMoviendo())
		{
			this.setSeEstaMoviendo(false);
			ControladorEditor.getInstancia().guardarMemento(this.memento);
//			System.out.println("Cambio guardado para deshacer - Evento Etiqueta eventos"); //TODO O U T
		}
		else 
			showPopUp(event);
	}

	private boolean isSeEstaMoviendo() {
		return seEstaMoviendo;
	}

	private void setSeEstaMoviendo(boolean seEstaMoviendo) {
		this.seEstaMoviendo = seEstaMoviendo;
	}

    private void showPopUp(MouseEvent event)
    {
    	PopupMenuEditar popup = graficable.getPopupMenu();
    	
        if (event.isPopupTrigger() && popup != null) 
          popup.show(event.getComponent(), event.getX(), event.getY());
    }
}
